package com.authine.cloudpivot.ext.applicationservice;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Path("BlackListService")
@Slf4j
public class BlackListService extends ApplicationService {
    /**
     * 获取供应商数据列表
     *
     * @return
     */
    @Path("getOrgTransferInfo")
    @Operation(summary = "获取供应商列表", tags = CUSTOM_SERVICE,description = "{id:必填}")
    @POST
    public SingleResponse getOrgTransferInfo(SelectorFormat client) {
        if (log.isDebugEnabled())
            log.debug("===================clientJsonString={}", client);
        if (client == null || StringUtils.isEmpty(client.getId())) {
            return SingleResponse.buildFailure("-1", "客户组织id不能为空");
        }
        //获取所有启用中的组织
//        StringBuilder sql = new StringBuilder("SELECT a.id as `key`,a.name as `title`  from app_commonservice.h_org_department a\n" +
//                "LEFT JOIN app_commonservice.h_muti_org  b  on a.corpId =  b.corpId\n" +
//                "where  1=1 ")
//                .append(" and b.orgStop  =0 and a.orgRoot = 1 ")
//                .append(" and a.id != '").append(client.getId()).append("'");
//
//        List<Map<String, Object>> orgList = JdbcTemplateUtils.queryForList(sql.toString(), new HashMap<>());

        List<Map<String, Object>> orgList = JdbcTemplateUtils.builder(AppConst.SYSTEM_MANAGEMENT, ModelConst.T_ORGANIZATION)
                .selectField(" id as `key`,name as `title` ")
                .eq("id", client.getId())
                .queryForList();

        if (log.isDebugEnabled()) {
            log.debug("orgList={}", orgList);
        }

        //获取当前组织的黑名单
        Map<String, Object> blackMap = JdbcTemplateUtils.builder(ModelConst.T_BLACK_LIST)
                .selectField(" id,organ_ids ")
                .eq("client_organid", client.getId())
                .queryForMap();
        if (blackMap == null) {
            blackMap = new HashMap<>();
        }
        String[] blackOrg = new String[]{};
        String organIds = (String) blackMap.get("organ_ids");
        if (StringUtils.isNotEmpty(organIds)) {
            blackOrg = organIds.split(",");
        }

        String blackId = (String) blackMap.get("id");

        Map<String, Object> build = MapBuilder.create(new HashMap<String, Object>())
                .put("id", blackId)
                .put("leftTransferList", orgList)
                .put("rightTransferList", blackOrg)
                .build();

        return SingleResponse.of(build);
    }


}
